﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LaughingDog.GameObjects;
using Microsoft.Xna.Framework;

namespace LaughingDog.Components
{
    /// <summary>
    /// The AudioListener component is used as essentially the players ears in game, there can only be one active at any point and it is generally attached to a Camera.
    /// </summary>
    public class AudioReceiver : Behaviour
    {
        private static AudioReceiver mActiveReceiver = null;

        private Single mMaxVolume;
        private Boolean mIgnoreSounds = false;

        private Microsoft.Xna.Framework.Audio.AudioListener mListener;

        /// <summary>
        /// Gets or sets the current active AudioListener.
        /// </summary>
        public static AudioReceiver ActiveReceiver { get { return mActiveReceiver; } set { mActiveReceiver = value; } }

        /// <summary>
        /// Gets or sets the max volume any sound can play at regardless of the closeness to the AudioListener.
        /// </summary>
        public Single MaxVolume { get { return mMaxVolume; } set { mMaxVolume = value; } }

        /// <summary>
        /// Gets or sets a value indicating whether this listener should ignore all 2D sounds.
        /// </summary>
        public Boolean IgnoreSounds { get { return mIgnoreSounds; } set { mIgnoreSounds = value; } }

        /// <summary>
        /// 
        /// </summary>
        public Microsoft.Xna.Framework.Audio.AudioListener Listener { get { return mListener; } }

        /// <summary>
        /// Creates a new instance of an AudioListener component.
        /// </summary>
        /// <param name="gameObject">The GameObject this component is attached to.</param>
        public AudioReceiver(GameObject gameObject)
            : base(gameObject)
        {
            mListener = new Microsoft.Xna.Framework.Audio.AudioListener();

            if (AudioReceiver.ActiveReceiver == null)
                AudioReceiver.ActiveReceiver = this;
        }

        /// <summary>
        /// Updates this AudioListener component.
        /// </summary>
        public override void Update()
        {
            mListener.Position = new Vector3(Transform.Position, 0);
            
            base.Update();
        }
    }
}
